﻿using System.Configuration;
using contSys.database.DAL;
using System.Data;

using contSys.Security;
using System;

namespace contSys.BL.Simulator
{
    public class PhaseParams
    {
        private static string _connString = ConfigurationManager.ConnectionStrings["Simulator"].ConnectionString;
        private static string tableObjName = "PhaseParams";

        public static string LastErrorMsg = string.Empty;

        public static DataSet GetByPlan(string usr, string pwd, object unitID, object planID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM PhaseParams WHERE (UnitID={0} AND PlanID={1})", unitID.ToString(), planID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }

        public static DataSet GetByPhase(string usr, string pwd, object unitID, object phaseID)
        {
            if (UserManager.QueryAllowed(tableObjName, usr, pwd))
            {
                LastErrorMsg = string.Empty;
                string sql = string.Format("SELECT * FROM PhaseParams WHERE (UnitID={0} AND PhaseID={1})", unitID.ToString(), phaseID.ToString());
                return SQLiteDB.GetRecords(_connString, sql);
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return null;
        }


        #region modify
        private static bool checkValidity(object unitID, object minPh, object maxPhLen, object moves)
        {
            int mp, mpl, rc;
            string[] ms = moves.ToString().Split(',');
            string sql;

            LastErrorMsg = string.Empty;
            sql = string.Format("SELECT MoveID FROM Moves WHERE (UnitID={0} AND MoveID IN ({1}))", unitID.ToString(), moves.ToString());
            if (!SQLiteDB.TryCountRecords(_connString, sql, out rc) || rc < ms.Length)
            {
                LastErrorMsg = string.Format("part-of or all phase moves [{0}] are invalid. moves should exist and must be seperated by commas only!", moves.ToString());
                return false;
            }

            if (!int.TryParse(minPh.ToString(), out mp) || mp < 0)
            {
                LastErrorMsg = "minPh should be a number >= 0";
                return false;
            }

            if (!int.TryParse(maxPhLen.ToString(), out mpl) || mpl<0)
            {
                LastErrorMsg = "maxPhLen should be a number >= 0";
                return false;
            }

            return true;
        }

        public static bool Modify(string usr, string pwd, object unitID, object planID, object PhaseID, object minPh, object maxPhLen, object moves)
        {
            if (UserManager.ModifyAllowed(tableObjName, usr, pwd))
            {
                if (checkValidity(unitID, minPh, maxPhLen, moves))
                {
                    LastErrorMsg = string.Empty;
                    string sql = string.Format("UPDATE PhaseParams SET minPh={0}, maxPhLen={1}, Moves='{2}' WHERE (UnitID={3} AND PlanID={4} AND PhaseID={5})", minPh.ToString(), maxPhLen.ToString(), moves.ToString(), unitID.ToString(), planID.ToString(), PhaseID.ToString());
                    return SQLiteDB.TryExecuteScalar(_connString, sql);
                }
            }
            else
            {
                LastErrorMsg = "Failed security";
            }
            return false;
        }
        #endregion
    }
}
